<template>
  <div class="root">
    <div class="divBox">
      <div class="content-container">
        <div class="container">
          <div style="width: 390px" class="selectbox-root">
            <span class="selectbox-hint" style="width: 60px">搜索</span>
            <div class="selectbox-deliver" />

            <el-input v-model="searchKeyword" placeholder="输入交易所名称" clearable size="medium"
              @keyup.enter.native="onClickSearch" />
          </div>
          <button style="margin-left: 16px" class="light-green-btn" @click="onClickSearch">
            查询
          </button>
        </div>
        <div>
          <el-table :header-cell-style="{
            background: '#F2F5F7',
            border: '0px solid #DDDDDD',
            color: '#242B35',
            height: '64px',
          }" :show-header="true" :data="list" :row-style="{ height: '64px' }" stripe style="width: 100%">
            <el-table-column min-width="10"></el-table-column>
            <el-table-column label="序号" align="left" min-width="60">
              <template slot-scope="scope"><span>{{ getCurListNo(scope.$index) }}</span></template>
            </el-table-column>
            <el-table-column :show-overflow-tooltip="true" prop="name" label="交易所名称" align="left" width="150" />
            <el-table-column align="left" prop="city" label="城市" width="100" />
            <el-table-column align="left" prop="businessScope" label="业务范围" width="500" />
            <el-table-column align="center" prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <span :class="scope.row.status === 1 ? 'status-published' : 'status-unpublished'">
                  {{ scope.row.status === 1 ? '已发布' : '未发布' }}
                </span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" min-width="170">
              <template slot-scope="scope">
                <a class="list-blue-text" @click="handle(scope.row.detailUrl)">查看</a>
                <a :class="editStyleChange(scope.row.status)" style="margin-left: 10px"
                  @click="onEdit(scope.row.website)">编辑</a>
                <a style="margin-left: 10px" :class="publishStyleChange(scope.row.status)"
                  @click="onClickPublish(scope.row.id)">发布</a>
                <a style="margin-left: 10px" @click="onClickOffline(scope.row.id)"
                  :class="offlineStyleChange(scope.row.status)">下线</a>
                <a style="margin-left: 10px" class="list-red-text" @click="onClickDelete(scope.row.id)">删除</a>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div style="margin-top: 30px; margin-bottom: 10px" class="pageBox">
        <div style="flex-grow: 1" />
        <a style="margin: auto" class="pageBox-total-num">共{{ total }}条</a>
        <el-pagination style="margin: auto" background @size-change="handleSizeChange"
          @current-change="handleCurrentChange" :current-page="current" :page-size="pageSize" :page-count="pageCount"
          layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { loadCarbonExchangeList } from "@/api/carbonAssetApi";
import { delCarbonExchanger } from "@/api/carbonAssetApi";
import { updateCarbonExchanger } from "@/api/carbonAssetApi";
import selectDropDownBox from "@/components/selectbox/selectDropDownBox.vue";
import { openUrlInNewWindow } from "@/libs/OpenHelper";
import { setListNo } from "@/libs/public";

export default {
  name: "exchangeManager",
  components: { selectDropDownBox },
  data() {
    return {
      searchKeyword: "",
      list: [],
      total: 0,
      current: 1,
      pageCount: 1,
      pageSize: 10,
      exchangesUrl:
        "https://carbonmsger.feishu.cn/drive/folder/fldcn66yo6D4OoXwZqEMHL6OQSg?from=space_persnoal_filelist",
    };
  },
  methods: {
    cellStyle({ column }) {
      if (column.label === "操作") {
        return "margin-left:0px;padding:0 0;";
      } else {
        return "cursor:pointer;";
      }
    },
    //按关键字搜索交易所
    onClickSearch() {
      this.current = 1; // 重置到第一页
      this.getList();
    },
    onEdit(url) {
      if (url) {
        openUrlInNewWindow(url);
      } else {
        this.$message.warning("没有对应的编辑链接");
      }
    },
    //判断是否发布，若发布了则修改样式
    editStyleChange(status) {
      return "list-blue-text";
    },

    publishStyleChange(status) {
      if (status === 1) {
        return "afterSubmitPublish";
      } else {
        return "list-blue-text";
      }
    },
    offlineStyleChange(status) {
      if (status === 1) {
        return "list-red-text";
      } else {
        return "afterSubmitOffline";
      }
    },
    onClickPublish(row_id) {
      const data = {
        id: parseInt(row_id),
        status: 1,
      };
      updateCarbonExchanger(data).then(
        () => {
          this.$message.success("发布成功");
          this.getList();
        },
        () => {
          this.$message.error("发布失败");
        }
      );
    },
    onClickDelete(id) {
      const deleteId = parseInt(id);
      this.$confirm("删除内容不可复原，请谨慎操作", "确认删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delCarbonExchanger(deleteId).then(
          () => {
            this.$message.success("删除成功");
            this.getList();
          },
          () => {
            this.$message.error("删除失败");
          }
        );
      }).catch(() => {
        this.$message.info("已取消删除");
      });
    },
    handle(url) {
      if (url) {
        openUrlInNewWindow(url);
      } else {
        this.$message.warning("没有对应的详情链接");
      }
    },
    getCurListNo(index) {
      let curNo = parseInt(index) + 1;
      let size = this.pageSize || 10;
      let page = this.current - 1;
      let no = setListNo(page, size, curNo);
      return no ? no : 1;
    },
    onClickOffline(row_id) {
      const data = {
        id: parseInt(row_id),
        status: 2,
      };
      updateCarbonExchanger(data).then(
        () => {
          this.$message.success("下线成功");
          this.getList();
        },
        () => {
          this.$message.error("下线失败");
        }
      );
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.current = 1; // 重置到第一页
      this.getList();
    },
    handleCurrentChange(val) {
      this.current = val;
      this.getList();
    },
    getList() {
      const data = {
        asc: true,
        current: this.current,
        size: this.pageSize,
        sortField: "",
      };
      
      // 如果有搜索关键词，添加到请求参数中
      if (this.searchKeyword) {
        data.searchKey = this.searchKeyword;
      }

      console.log("发送请求数据:", data);
      loadCarbonExchangeList(data)
        .then((res) => {
          console.log("API响应数据:", res);
          this.list = res.data.records || [];
          this.total = parseInt(res.data.total) || 0;
          this.current = parseInt(res.data.current) || 1;
          this.pageCount = Math.ceil(this.total / this.pageSize);
          
          // 处理数据，确保字段不为空
          this.list.forEach((v) => {
            for (const i in v) {
              if (v[i] === null || v[i] === undefined || v[i] === "") {
                v[i] = "--";
              }
            }
          });
        })
        .catch((error) => {
          console.error("获取数据失败:", error);
          this.$message.error("获取数据失败");
        });
    },
  },
  mounted() {
    this.getList();
  },
  };
  </script>
  <style lang="scss" scoped>
  .root {
    background: #f2f5f7;
  }
  
  .divBox {
    margin: 20px;
    background: #ffffff;
    box-shadow: 0 2px 8px 0 #eaf0f3;
    border-radius: 8px;
  }
  
  .container {
    margin: 10px 0 20px 0;
    display: flex;
    flex-direction: row;
  }
  
  .content-container {
    display: flex;
    flex-direction: column;
    width: 100%;
  }
  
  >>>.el-cascader .el-input .el-input__inner,
  >>>.el-cascader .el-input.is-focus .el-input__inner {
    border-color: transparent;
  }
  
  .acea-row {
    /deep/.el-avatar--small {
      width: 22px;
      height: 22px;
      
    }
  }
  
  .checkTime {
    /deep/.el-radio__input {
      display: none;
    }
  }
  
  .ivu-pl-8 {
    margin-left: 8px;
    
  }
  
  .dashboard-console-visit {
    /deep/.el-card__header {
      padding: 14px 20px !important;
    }
  
    ul {
      li {
        list-style-type: none;
        margin-top: 12px;
      }
    }
  }
  
  .ivu-mb {
    margin-bottom: 10px;
  }
  
  .newsImg {
    width: 30px;
    height: 30px;
    border-radius: 4px;
  }

  .status-published {
    color: #67c23a;
    font-weight: bold;
  }

  .status-unpublished {
    color: #f56c6c;
    font-weight: bold;
  }
  </style>